/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison.websitereader.projectorquest;

import java.io.IOException;
import java.util.ArrayList;
import jxl.write.WriteException;
import pricecomparison.PriceComparisonView;
import pricecomparison.websitereader.WebsiteReader;

/**
 *
 * @author Jim Simon
 */
public class PQ_ItemReader extends PQ_HomePageReader{
    String URL = super.URL;
    WebsiteReader reader;
    
    public PQ_ItemReader(String url) throws java.net.MalformedURLException, java.io.IOException {
        URL = url;
        try {
            reader = new WebsiteReader(URL);
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }

    }
    
    public void beginSearch() throws jxl.write.WriteException, java.io.IOException {
        String name, oemPrice, compatiblePrice, modelNumber;
        try {
            name = getItemName();
            modelNumber = getModelNumber();
            String[] prices = getPrices();
            compatiblePrice = prices[0];
            oemPrice = prices[1];
            reader.close();
        } catch (java.io.IOException e) {
            throw e;
        }
            
        if (reader.DEBUG) {
            System.out.println("--- Name: " + name);
            System.out.println("--- Model Number: " + modelNumber);
            System.out.println("--- Compatible Price: " + compatiblePrice);
            System.out.println("--- OEM Price: " + oemPrice);
        }

        ArrayList<String> items = new ArrayList();
        items.add(name);
        items.add(modelNumber);
        items.add(compatiblePrice);
        items.add(oemPrice);
        try {
            PriceComparisonView.excelWriter.writeItem(items, PriceComparisonView.excelWriter.MFER_PROJECTORQUEST);
        } catch (jxl.write.WriteException e) {
            throw e;
        }
    }
    
    public String getItemName() throws java.io.IOException {
        String line;
        String key = "<div class=\"product-name\">";
        
        try {
            line = reader.readLine();
            while (!line.contains(key))
                line = reader.readLine();

            key = "<h1>";
            while (!line.contains(key))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        // Find beginning portion
        int pos = line.indexOf(key);
        line = line.substring(pos + key.length());
        
        // Find end portion
        key = "</h1>";
        pos = line.indexOf(key);
        line = line.substring(0, pos);    
        
        return line;
    }
    
    public String getModelNumber() throws java.io.IOException {
        String line;
        String key = "<div class=\"product-name\">";
        
        try {
            line = reader.readLine();
        
            while (!line.contains(key))
                line = reader.readLine();

            key = "</div>";
            while (!line.contains(key))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        // Find beginning portion
        int pos = line.indexOf(key);
        line = line.substring(0, pos);    
        
        return line.trim().replace("Model Number :", "");
    }
    
    public String[] getPrices() throws java.io.IOException {
        String line;
        
        try {
            line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        String key;
        String[] prices = new String[2];
        boolean OEM = false;
        
        try {
            while (!line.contains("Genuine Compatible") && !line.contains("OEM") && !line.contains("class=\"price\">"))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
      
        if (line.contains("OEM"))
            OEM = true;
        
        // Find beginning portion
        key = "class=\"price\">";
        try {
            while (!line.contains(key))
                line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        int pos = line.indexOf(key);
        line = line.substring(pos + key.length());

        // Find end portion
        key = "</span></span>";
        pos = line.indexOf(key);
        line = line.substring(0, pos); 
        
        if (!OEM) {
            prices[0] = line;
            try {
                while (!line.contains("Genuine Compatible") && !line.contains("OEM")) {
                    if (line.contains("<div class=\"clearer\">")) {
                        prices[1] = "-";
                        break;
                    }
                    line = reader.readLine();
                }
                
                if (prices[1] != "-") {
                    // Find beginning portion
                    key = "class=\"price\">";             
                    while (!line.contains(key))
                        line = reader.readLine();
                    pos = line.indexOf(key);
                    line = line.substring(pos + key.length());
                    
                    // Find end portion
                    key = "</span></span>";
                    pos = line.indexOf(key);
                    prices[1] = line.substring(0, pos); 
                }
            } catch (java.io.IOException e) {
                throw e;
            }
        }
        else {
            prices[0] = "-";
            prices[1] = line;
        }
        
        return prices;
    }
}
